<?php

namespace app\common\model\banxi\say;

use app\common\enums\say\SayCategoryStatusEnum;
use app\common\enums\say\SayCategoryTypeEnum;
use app\common\model\common\BaseModel;
use think\db\exception\PDOException;
use think\Exception;
use think\exception\ValidateException;
use think\Model;
use think\model\concern\SoftDelete;

/**
 * 学员
 */
class SayCategory extends BaseModel
{
    use SoftDelete;

    protected $name = 'banxi_say_category';

    protected $autoWriteTimestamp = true;

    protected $deleteTime = 'del';

    protected $defaultSoftDelete = 0;

    protected $append = [
        'typeCn',
        'statusCn'
    ];

    protected $type = [
        'id' => 'string',
        'del' => 'string',
        'type' => 'string',
        'createTime' => 'timestamp:Y-m-d H:i:s',
        'updateTime' => 'timestamp:Y-m-d H:i:s',
    ];

    //-----------------------------------------------------------------------------------
    //-                                    获取器                                      -
    //-----------------------------------------------------------------------------------
    public function getTypeCnAttr(int|null $value, array $data): string
    {
        $value = $value ?: ($data['type'] ?? '');
        return (new SayCategoryTypeEnum())->getDescription($value) ?? '';
    }

    public function getStatusCnAttr(int|null $value, array $data): string
    {
        $value = $value ?: ($data['status'] ?? '');
        return (new SayCategoryStatusEnum())->getDescription($value) ?? '';
    }

    //-----------------------------------------------------------------------------------
    //-                                    处理方法                                      -
    //-----------------------------------------------------------------------------------

    //获取列表
    public function getListPage(array $where, string $page = '1', int $limit = 10): \think\Paginator
    {
        return self::where($where)
            ->order(['sortNum' => 'asc'])
            ->paginate([
                'page' => $page,
                'list_rows' => $limit,
            ]);

    }

    //获取列表(用于下拉选择)
    public function getListSelectPage(array $where, string $page = '1', int $limit = 10): \think\Paginator
    {
        return self::where($where)
            ->order(['sortNum' => 'asc'])
            ->field('id,name,type')
            ->paginate([
                'page' => $page,
                'list_rows' => $limit,
            ]);

    }

    //获取信息
    public function getInfo(array $where): array|Model|null
    {
        return self::where($where)
            ->order(['id' => 'desc'])
            ->find();
    }

    /**
     * 新增/更新
     * @param $organizationId
     * @param $data
     * @param $dealUser
     * @return true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function saveOrUpdate(array $data): bool
    {
        try {
            $id = $data['id'] ?? 0;
            $saveData = $data;
            if (!$id) {
                $this->save($saveData);
            } else {
                $info = self::where('id', $id)->find();
                if (empty($info)) {
                    throw new ValidateException('年级阶段不存在');
                }
                $info->save($saveData);
            }
        } catch (ValidateException|PDOException|Exception $e) {
            throw new ValidateException($e->getMessage());
        }
        return true;
    }

    public function del($ids)
    {
        return self::destroy(function ($query) use ($ids) {
            $query->whereIn('id', $ids);
        });
    }
    //-----------------------------------------------------------------------------------
    //-                                    模型关联                                      -
    //-----------------------------------------------------------------------------------


}